package com.dhecp.project.logstics.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.logstics.domain.FbaWarehousingDetail;
import com.dhecp.project.logstics.mapper.FbaWarehousingDetailMapper;
import com.dhecp.project.logstics.service.FbaWarehousingDetailService;
import com.dhecp.project.storage.domain.HwTailprogramdetailTol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FbaWarehousingDetailServiceImpl extends BaseModuleServiceImpl implements FbaWarehousingDetailService {
    @Autowired
    private FbaWarehousingDetailMapper fbaWarehousingDetailMapper;

    @Override
    public List<FbaWarehousingDetail> queryAutoList(Map<String, Object> params) throws Exception {
        List<FbaWarehousingDetail> queryList = new ArrayList<>();
        List<FbaWarehousingDetail> fbaWarehousingDetailList = fbaWarehousingDetailMapper.queryByCode(params.get("code").toString());
        List<FbaWarehousingDetail> fbaWarehousingDetailNew = null;
        String numRealityDeliver;
        String numRealityDeliver1;
        //总的数量值
        HwTailprogramdetailTol hwTailprogramdetailTol = new HwTailprogramdetailTol();
        Map<String,List<BigDecimal>> boxListMap = new HashMap<>();
        List<BigDecimal> boxList;
        List<BigDecimal> boxListTol;
        List<String> zzbh = new ArrayList<>();


        if (fbaWarehousingDetailList.size() > 0){
            //取出每条箱子的商品数据将箱数赋值到对应的箱子数据上
            for (int j = 0; j < fbaWarehousingDetailList.size(); j++) {
                fbaWarehousingDetailNew = JSON.parseArray(fbaWarehousingDetailList.get(j).getProductInfo(), FbaWarehousingDetail.class);
                for (int i = 0; i < fbaWarehousingDetailNew.size(); i++) {
                    boxList = new LinkedList<>();
                    numRealityDeliver = fbaWarehousingDetailNew.get(i).getNumRealityDeliver().equals("") ? "0" : fbaWarehousingDetailNew.get(i).getNumRealityDeliver();
                    if (j != 0) {
                        //计算商品的计划数量和实际数量
                        numRealityDeliver1 = queryList.get(i).getNumRealityDeliver().equals("")?"0":queryList.get(i).getNumRealityDeliver();
                        queryList.get(i).setNumRealityDeliver(
                                String.valueOf(new Integer(numRealityDeliver1)
                                        + new Integer(numRealityDeliver)));
                    }
                    //将每个商品的箱子数据按照数组形式存储起来
                    if (boxListMap.get(fbaWarehousingDetailNew.get(i).getSerial()) == null) {
                        boxList.add(new BigDecimal(numRealityDeliver));
                        boxListMap.put(fbaWarehousingDetailNew.get(i).getSerial(), boxList);
                    } else {
                        boxList = boxListMap.get(fbaWarehousingDetailNew.get(i).getSerial());
                        if (fbaWarehousingDetailNew.get(i).getNumRealityDeliver().equals("")) {
                            boxList.add(BigDecimal.ZERO);
                        } else {
                            boxList.add(new BigDecimal(fbaWarehousingDetailNew.get(i).getNumRealityDeliver()));
                        }
                        boxListMap.put(fbaWarehousingDetailNew.get(i).getSerial(), boxList);
                    }
                    if (j == 0) {
                        fbaWarehousingDetailNew.get(i).setBoxList("");
                        queryList.add(fbaWarehousingDetailNew.get(i));
                    }
                }

                //箱重-追踪编号的每箱的数据设置进数组中
                String serial = "";
                String val = "";
                for (int i = 1; i < 2; i++) {
                    boxListTol = new LinkedList<>();
                    switch (i) {
                        case 1:
                            serial = "追踪编号";
                            if (fbaWarehousingDetailList.get(j).getTrackingCode() == null){
                                val = "0";
                                zzbh.add("0");
                            }else {
                                val = fbaWarehousingDetailList.get(j).getTrackingCode();
                                zzbh.add(fbaWarehousingDetailList.get(j).getTrackingCode());
                            }
                            break;
                    }
                    if (j == 0) {
                        boxListMap.put(serial, boxListTol);
                    } else {
                        boxListTol = boxListMap.get(serial);
                        if (val.equals("")) {
                            boxListTol.add(BigDecimal.ZERO);
                            zzbh.add("0");
                        } else {
                            if (!serial.equals("追踪编号")){
                                boxListTol.add(new BigDecimal(val));
                            }
                        }
                        boxListMap.put(serial, boxListTol);
                    }
                }
            }

            //进行排序（naturalOrder 正序）（reverseOrder倒叙）
            queryList = queryList.stream().
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCommodityCode, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCodeStockLocation, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCodeStockArea, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCodeStockOn, Comparator.naturalOrder())).
                    collect(Collectors.toList());

            Map<String,BigDecimal> boxListMapTol = new HashMap<>();
            //合计栏数据
            for (FbaWarehousingDetail fbaWarehousingDetail : queryList){
                fbaWarehousingDetail.setBoxList(boxListMap.get(fbaWarehousingDetail.getSerial()).toString());
                hwTailprogramdetailTol.setNumPlanDeliver(String.valueOf(new Integer(hwTailprogramdetailTol.getNumPlanDeliver()) + new Integer(fbaWarehousingDetail.getNumPlanDeliver())));
                hwTailprogramdetailTol.setFbaWarehousingNum(String.valueOf(new Integer(hwTailprogramdetailTol.getFbaWarehousingNum()) + (fbaWarehousingDetail.getFbaWarehousingNum().equals("")?0:new Integer(fbaWarehousingDetail.getFbaWarehousingNum()))));
                hwTailprogramdetailTol.setNumRealityDeliver(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver()) + (fbaWarehousingDetail.getNumRealityDeliver().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver()))));
                for (int i = 0; i < boxListMap.get(fbaWarehousingDetail.getSerial()).size(); i++) {
                    if (boxListMapTol.get(String.valueOf(i)) == null){
                        boxListMapTol.put(String.valueOf(i),boxListMap.get(fbaWarehousingDetail.getSerial()).get(i));
                    }else {
                        if (boxListMapTol.get(String.valueOf(i)).equals("")){
                            boxListMapTol.put(String.valueOf(i),BigDecimal.ZERO);
                        }else {
                            boxListMapTol.put(String.valueOf(i),boxListMapTol.get(String.valueOf(i)).add(boxListMap.get(fbaWarehousingDetail.getSerial()).get(i)));
                        }
                    }
                }
            }

            //设置合计数据
            boxList = new LinkedList<>();
            for(Map.Entry entry : boxListMapTol.entrySet()){
                boxList.add((BigDecimal) entry.getValue());
            }
            hwTailprogramdetailTol.setBoxList(boxList.toString());
        }



        FbaWarehousingDetail fbaWarehousingDetail;


        String serial = "";
        for (int i = 0; i < 2; i++) {
            fbaWarehousingDetail = new FbaWarehousingDetail();
            switch (i) {
                case 0:
                    serial = "合计";
                    break;
                case 1:
                    serial = "追踪编号";
                    break;
            }
            if (i != 0){
                if (i == 1){
                    fbaWarehousingDetail.setBoxList(zzbh==null?null:zzbh.toString());
                }else {
                    fbaWarehousingDetail.setBoxList(boxListMap.get(serial)==null?null:boxListMap.get(serial).toString());
                }
            }
            if (i == 0){
                fbaWarehousingDetail.setNumPlanDeliver(hwTailprogramdetailTol.getNumPlanDeliver());
                fbaWarehousingDetail.setFbaWarehousingNum(hwTailprogramdetailTol.getFbaWarehousingNum().equals("0")?"":hwTailprogramdetailTol.getFbaWarehousingNum());
                fbaWarehousingDetail.setNumRealityDeliver(hwTailprogramdetailTol.getNumRealityDeliver().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver());
                fbaWarehousingDetail.setBoxList(hwTailprogramdetailTol.getBoxList());
            }

            fbaWarehousingDetail.setScrapData("1");
            fbaWarehousingDetail.setSerial(serial);
            queryList.add(fbaWarehousingDetail);
        }
        return queryList;
    }

    @Override
    public List<FbaWarehousingDetail> queryList2(Map<String, Object> params) throws Exception {
        List<FbaWarehousingDetail> queryList = new ArrayList<>();
        List<FbaWarehousingDetail> fbaWarehousingDetailList = fbaWarehousingDetailMapper.queryByCode(params.get("code").toString());
        List<FbaWarehousingDetail> fbaWarehousingDetailNew = null;
        String numRealityDeliver;
        String numRealityDeliver1;
        //总的数量值
        HwTailprogramdetailTol hwTailprogramdetailTol = new HwTailprogramdetailTol();
        if (fbaWarehousingDetailList.size() > 0){
            //取出每条箱子的商品数据将箱数赋值到对应的箱子数据上
            for (int j = 0; j < fbaWarehousingDetailList.size(); j++) {
                fbaWarehousingDetailNew = JSON.parseArray(fbaWarehousingDetailList.get(j).getProductInfo(), FbaWarehousingDetail.class);
                for (int i = 0; i < fbaWarehousingDetailNew.size(); i++) {
                    if (j == 0) {
                        switch (j) {
                            case 0:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver1(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 1:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver2(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 2:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver3(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 3:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver4(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 4:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver5(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 5:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver6(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 6:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver7(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 7:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver8(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 8:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver9(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 9:
                                fbaWarehousingDetailNew.get(i).setNumRealityDeliver10(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                        }
                    } else {
                        numRealityDeliver = fbaWarehousingDetailNew.get(i).getNumRealityDeliver().equals("")?"0":fbaWarehousingDetailNew.get(i).getNumRealityDeliver();
                        numRealityDeliver1 = queryList.get(i).getNumRealityDeliver().equals("")?"0":queryList.get(i).getNumRealityDeliver();
                        queryList.get(i).setNumRealityDeliver(
                                String.valueOf(new Integer(numRealityDeliver1)
                                        + new Integer(numRealityDeliver)));
                        switch (j) {
                            case 1:
                                queryList.get(i).setNumRealityDeliver2(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 2:
                                queryList.get(i).setNumRealityDeliver3(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 3:
                                queryList.get(i).setNumRealityDeliver4(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 4:
                                queryList.get(i).setNumRealityDeliver5(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 5:
                                queryList.get(i).setNumRealityDeliver6(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 6:
                                queryList.get(i).setNumRealityDeliver7(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 7:
                                queryList.get(i).setNumRealityDeliver8(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 8:
                                queryList.get(i).setNumRealityDeliver9(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                            case 9:
                                queryList.get(i).setNumRealityDeliver10(fbaWarehousingDetailNew.get(i).getNumRealityDeliver());
                                break;
                        }
                    }
                    if (j == 0) {
                        queryList.add(fbaWarehousingDetailNew.get(i));
                    }
                }
            }
            //进行排序（naturalOrder 正序）（reverseOrder倒叙）
            queryList = queryList.stream().
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCommodityCode, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCodeStockLocation, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCodeStockArea, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaWarehousingDetail::getCodeStockOn, Comparator.naturalOrder())).
                    collect(Collectors.toList());
        }
        //合计栏数据
        for (FbaWarehousingDetail fbaWarehousingDetail : queryList){
            hwTailprogramdetailTol.setFbaWarehousingNum(String.valueOf(new Integer(hwTailprogramdetailTol.getFbaWarehousingNum()) + new Integer(fbaWarehousingDetail.getFbaWarehousingNum().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver()))));
            hwTailprogramdetailTol.setNumPlanDeliver(String.valueOf(new Integer(hwTailprogramdetailTol.getNumPlanDeliver()) + new Integer(fbaWarehousingDetail.getNumPlanDeliver())));
            hwTailprogramdetailTol.setNumRealityDeliver(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver()) + (fbaWarehousingDetail.getNumRealityDeliver().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver()))));
            hwTailprogramdetailTol.setNumRealityDeliver1(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver1()) + (fbaWarehousingDetail.getNumRealityDeliver1().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver1()))));
            hwTailprogramdetailTol.setNumRealityDeliver2(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver2()) + (fbaWarehousingDetail.getNumRealityDeliver2().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver2()))));
            hwTailprogramdetailTol.setNumRealityDeliver3(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver3()) + (fbaWarehousingDetail.getNumRealityDeliver3().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver3()))));
            hwTailprogramdetailTol.setNumRealityDeliver4(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver4()) + (fbaWarehousingDetail.getNumRealityDeliver4().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver4()))));
            hwTailprogramdetailTol.setNumRealityDeliver5(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver5()) + (fbaWarehousingDetail.getNumRealityDeliver5().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver5()))));
            hwTailprogramdetailTol.setNumRealityDeliver6(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver6()) + (fbaWarehousingDetail.getNumRealityDeliver6().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver6()))));
            hwTailprogramdetailTol.setNumRealityDeliver7(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver7()) + (fbaWarehousingDetail.getNumRealityDeliver7().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver7()))));
            hwTailprogramdetailTol.setNumRealityDeliver8(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver8()) + (fbaWarehousingDetail.getNumRealityDeliver8().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver8()))));
            hwTailprogramdetailTol.setNumRealityDeliver9(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver9()) + (fbaWarehousingDetail.getNumRealityDeliver9().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver9()))));
            hwTailprogramdetailTol.setNumRealityDeliver10(String.valueOf(new Integer(hwTailprogramdetailTol.getNumRealityDeliver10()) + (fbaWarehousingDetail.getNumRealityDeliver10().equals("")?0:new Integer(fbaWarehousingDetail.getNumRealityDeliver10()))));
        }

        FbaWarehousingDetail fbaWarehousingDetail;
        String serial = "";
        for (int i = 0; i < 2; i++) {
            fbaWarehousingDetail = new FbaWarehousingDetail();
            switch (i) {
                case 0:
                    serial = "合计";
                    break;
                case 1:
                    serial = "追踪编号";
                    break;
            }
            //添加箱重/长宽高等数据
            addJx(fbaWarehousingDetailList,fbaWarehousingDetail,i,hwTailprogramdetailTol);

            fbaWarehousingDetail.setScrapData("1");
            fbaWarehousingDetail.setSerial(serial);
            queryList.add(fbaWarehousingDetail);
        }
        return queryList;
    }

    private void  addJx(List<FbaWarehousingDetail> fbaWarehousingDetailList,FbaWarehousingDetail fbaWarehousingDetail,Integer type,HwTailprogramdetailTol hwTailprogramdetailTol){
        String valueNum = "";
        for (int j = 0; j < fbaWarehousingDetailList.size(); j++) {
            if (type == -1){
                valueNum = fbaWarehousingDetail.getNumRealityDeliver();
            }  else if (type == 1){
                valueNum = fbaWarehousingDetailList.get(j).getTrackingCode();
            }
            if (type != 0){
                switch (j) {
                    case 0:
                        fbaWarehousingDetail.setNumRealityDeliver1(valueNum);
                        break;
                    case 1:
                        fbaWarehousingDetail.setNumRealityDeliver2(valueNum);
                        break;
                    case 2:
                        fbaWarehousingDetail.setNumRealityDeliver3(valueNum);
                        break;
                    case 3:
                        fbaWarehousingDetail.setNumRealityDeliver4(valueNum);
                        break;
                    case 4:
                        fbaWarehousingDetail.setNumRealityDeliver5(valueNum);
                        break;
                    case 5:
                        fbaWarehousingDetail.setNumRealityDeliver6(valueNum);
                        break;
                    case 6:
                        fbaWarehousingDetail.setNumRealityDeliver7(valueNum);
                        break;
                    case 7:
                        fbaWarehousingDetail.setNumRealityDeliver8(valueNum);
                        break;
                    case 8:
                        fbaWarehousingDetail.setNumRealityDeliver9(valueNum);
                        break;
                    case 9:
                        fbaWarehousingDetail.setNumRealityDeliver10(valueNum);
                        break;
                }
            }
        }
        if (type == 0){
            fbaWarehousingDetail.setNumPlanDeliver(hwTailprogramdetailTol.getNumPlanDeliver());
            fbaWarehousingDetail.setFbaWarehousingNum(hwTailprogramdetailTol.getFbaWarehousingNum());
            fbaWarehousingDetail.setNumRealityDeliver(hwTailprogramdetailTol.getNumRealityDeliver().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver());
            fbaWarehousingDetail.setNumRealityDeliver1(hwTailprogramdetailTol.getNumRealityDeliver1().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver1());
            fbaWarehousingDetail.setNumRealityDeliver2(hwTailprogramdetailTol.getNumRealityDeliver2().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver2());
            fbaWarehousingDetail.setNumRealityDeliver3(hwTailprogramdetailTol.getNumRealityDeliver3().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver3());
            fbaWarehousingDetail.setNumRealityDeliver4(hwTailprogramdetailTol.getNumRealityDeliver4().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver4());
            fbaWarehousingDetail.setNumRealityDeliver5(hwTailprogramdetailTol.getNumRealityDeliver5().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver5());
            fbaWarehousingDetail.setNumRealityDeliver6(hwTailprogramdetailTol.getNumRealityDeliver6().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver6());
            fbaWarehousingDetail.setNumRealityDeliver7(hwTailprogramdetailTol.getNumRealityDeliver7().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver7());
            fbaWarehousingDetail.setNumRealityDeliver8(hwTailprogramdetailTol.getNumRealityDeliver8().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver8());
            fbaWarehousingDetail.setNumRealityDeliver9(hwTailprogramdetailTol.getNumRealityDeliver9().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver9());
            fbaWarehousingDetail.setNumRealityDeliver10(hwTailprogramdetailTol.getNumRealityDeliver10().equals("0")?"":hwTailprogramdetailTol.getNumRealityDeliver10());
        }
    }
}
